package com.magina.antiPro.redisMQ;

import com.alibaba.fastjson.JSON;
import com.magina.antiPro.model.Members;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("redis")
public class RedisPublishController {
    private int m = 0;
    @Autowired
    private StringRedisTemplate template;

    @RequestMapping("publish")
    public String publish() throws InterruptedException {
        int index = m;
        for (int i = m; i < index + 3; i++) {
            template.convertAndSend("mytopic", "这是我发第" + i + "条的消息啊");
        }
        Thread.sleep(2000);
        List<Members> membersList = Lists.newArrayList();
        Members members = new Members();
        Members members2 = new Members();
        members.setEmail("asdasd@qq.com");
        members.setMobile("12312131");
        members.setName("张三");
        members2.setEmail("asdasd2@qq.com");
        members2.setMobile("123121312");
        members2.setName("张三2");
        membersList.add(members);
        membersList.add(members2);
        template.convertAndSend("mytopic", JSON.toJSONString(membersList));
        template.convertAndSend("mytopicThree", "this msg send to mytopicThree,receiver should be receiveMessageThree!");
        return "结束";
    }
}
